package com.ibeeking.found.upms.b.rest.controller.v1;

import com.ibeeking.found.common.annotation.LoginUser;
import com.ibeeking.found.common.constants.GlobalConstant;
import com.ibeeking.found.common.constants.HeaderNounConstant;
import com.ibeeking.found.common.entity.CurrentUser;
import com.ibeeking.found.upms.b.rest.service.ICpPermissionService;
import com.ibeeking.found.upms.service.common.vo.CpPermissionListVO;
import com.ibeeking.found.upms.service.common.vo.CurrentPermissionVO;
import com.ibeeking.nematos.constants.enums.OperateLogTypeEnum;
import com.ibeeking.nematos.log.annotation.Log;
import com.ibeeking.nematos.utils.data.Collections3;
import com.ibeeking.nematos.utils.data.TreeFastUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * @ClassName PermissionController控制器
 * @Description
 * @Author ibeeking
 * @Date 2020-12-01 18:15
 **/
@RestController
@RequestMapping("/b/cpPermission/v1")
public class CpPermissionController {

    @Resource
    private ICpPermissionService cpPermissionServiceImpl;

    @Log(logType = OperateLogTypeEnum.QUERY, describe = "查询权限菜单树")
    @GetMapping(value = "/queryCpList")
    public List<CpPermissionListVO> queryCpList() {
        return cpPermissionServiceImpl.queryCpList();
    }

    @Log(logType = OperateLogTypeEnum.QUERY, describe = "查询所有有效菜单树")
    @GetMapping(value = "/queryAllEffectiveTreeList")
    public List<CurrentPermissionVO> queryAllEffectiveList() {
        List<CurrentPermissionVO> currentPermissionVos = cpPermissionServiceImpl.queryAllEffectiveTreeList();
        if (Collections3.isEmpty(currentPermissionVos)) {
            return Collections3.emptyList();
        }
        return TreeFastUtils.listToTree(currentPermissionVos, GlobalConstant.PARENT_ID);
    }

    @Log(logType = OperateLogTypeEnum.QUERY, describe = "查询有效权限菜单树")
    @GetMapping(value = "/queryEffectiveTreeList")
    public List<CurrentPermissionVO> queryEffectiveList(@LoginUser(isFull = true) CurrentUser currentUser, HttpServletRequest request) {
        List<CurrentPermissionVO> currentPermissionVos = cpPermissionServiceImpl.queryEffectiveTreeList(currentUser, request.getHeader(HeaderNounConstant.JWT_JTI));
        if (Collections3.isEmpty(currentPermissionVos)) {
            return Collections3.emptyList();
        }
        return TreeFastUtils.listToTree(currentPermissionVos, GlobalConstant.PARENT_ID);
    }

}
